package com.yc.thread.pro2_knn.group;

import com.yc.thread.pro2_knn.bean.BankMarketing;
import com.yc.thread.pro2_knn.bean.Distance;
import com.yc.thread.pro2_knn.bean.Sample;
import com.yc.thread.pro2_knn.knn.DistanceCalculator;
import com.yc.thread.pro2_knn.knn.EuclideanDistanceCalculator;

import java.util.List;

/**
 * @author zwei
 * @create 2024-07-19 20:26
 */
public class GroupDistanceTask implements Runnable{
    private Distance [] distances;
    private int startIndex;
    private int endIndex;
    private List<BankMarketing> dataSet;
    private Sample example;
    private DistanceCalculator distanceCalculator =new EuclideanDistanceCalculator();

    public GroupDistanceTask(Distance[] distances, int startIndex, int endIndex, List<BankMarketing> dataSet, Sample example,EuclideanDistanceCalculator euclideanDistanceCalculator) {
        this.distances = distances;
        this.startIndex = startIndex;
        this.endIndex = endIndex;
        this.dataSet = dataSet;
        this.example = example;
        this.distanceCalculator =euclideanDistanceCalculator;
    }
    public GroupDistanceTask(Distance[] distances, int startIndex, int endIndex, List<BankMarketing> dataSet, Sample example) {
        this.distances = distances;
        this.startIndex = startIndex;
        this.endIndex = endIndex;
        this.dataSet = dataSet;
        this.example = example;
    }

    public void setDistanceCalculator(DistanceCalculator distanceCalculator) {
        this.distanceCalculator = distanceCalculator;
    }

    @Override
    public void run() {
        for (int i = startIndex; i < endIndex; i++) {
            Sample sample=dataSet.get(i);
            //计算sample 与测试数据 example的距离
            distances[i]=new Distance();
            distances[i].setIndex(i);
            distances[i].setDistance(this.distanceCalculator.calculate(sample,example));
        }
    }
}
